﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PipeScript : MonoBehaviour {

    [SerializeField]
    private string animalTag;      //管道对应动物的tag
    private bool moving = false;   //管道是否正在移动

    private AudioSource sound;    //声音组件
    public AudioClip[] clips; //声音片段
    
    void Start ()
    {
        sound = GetComponent<AudioSource>();
    }
	
	
	void Update ()
    {
       //鼠标点击
       // if (Application.platform == RuntimePlatform.WindowsPlayer)
       // {
            if (Input.GetMouseButtonDown(0) && !GameManager.instance.isGameOver)
         //   {
                DetectTap();
        //    }
       // }
      //  else
      //  {
       //     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && !GameManager.instance.isGameOver)
      //      {
       //         DetectTap();
      //      }
      //  }
        //位置到边缘就到另一边
        if (transform.position.x > 5.25f)
        {
            transform.position = new Vector3(-5.25f, transform.position.y);
        }

        if (transform.position.x < -5.25f)
        {
            transform.position = new Vector3(5.25f, transform.position.y);
        }
    }

   
    void OnTriggerEnter2D(Collider2D other)
    {
        //判断是否tag相同
        if (other.CompareTag(animalTag))
        {
            //相同则加分
            sound.PlayOneShot(clips[0]);
            other.gameObject.SetActive(false);
            transform.GetChild(0).GetComponent<PipeAnim>().playAnim = true;
            GameManager.instance.currentScore++;
        }
        else
        {
            //不相同则游戏结束
            sound.PlayOneShot(clips[1]);
            GameManager.instance.isGameOver = true;
            CameraShake.instance.ShakeCamera(0.05f, 1f);
        }
    }

    //鼠标点下检测
    void DetectTap()
    {
        Vector2 pos;
       // if(Application.platform==RuntimePlatform.WindowsPlayer)
            pos = Input.mousePosition;
        //else
        //    pos = Input.GetTouch(0).position;
        //左边
        if (pos.x > 0 && pos.x < Screen.width / 2)
        {
            MoveLeft();
        }
        //右边
        else if (pos.x > Screen.width / 2 && pos.x < Screen.width)
        {
            MoveRight();
        }
    }

    //管道右移
    void MoveRight()
    {
        Vector3 lastPos = transform.position;
        Vector3 newPos = new Vector3(lastPos.x + 1.5f, lastPos.y);

        StartCoroutine(MoveFromTo(lastPos, newPos, 0.2f));
    }

    //管道左移
    void MoveLeft()
    {
        Vector3 lastPos = transform.position;
        Vector3 newPos = new Vector3(lastPos.x - 1.5f, lastPos.y);

        StartCoroutine(MoveFromTo(lastPos, newPos, 0.2f));
    }

    //差值移动
    IEnumerator MoveFromTo(Vector3 pointA, Vector3 pointB, float time)
    {
        if (!moving)
        {                     
            moving = true;                
            float t = 0f;
            while (t < 1.0f)
            {
                t += Time.deltaTime / time; 
                transform.position = Vector3.Lerp(pointA, pointB, t); 
                yield return null;         
            }
            moving = false;             
        }
    }
}
